﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HXDNB.CMS.App_Code;
using HXDNB.Infrastructure;
using HXDNB.DAL;

namespace HXDNB.CMS.Controllers
{
    public class CustomerController : BaseController
    {
        CustomerBLL bll = new CustomerBLL();

        public ActionResult List(CustomerType? Type)
        {
            var list = bll.GetList(Type);
            var pag = new Paging<Customer>(list);
            ViewBag.PagList = pag;
            return View();
        }

        public ActionResult Info(int? id)
        {
            var info = new Customer();
            if (id != null)
            {
                info = bll.GetByKey((int)id);
            }
            return View(info);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Info(Customer info)
        {
            bool result = false;
            if (info.ID > 0)
            {
                result = bll.update(info);
            }
            else
            {
                info.Createtime = DateTime.Now;
                info.ID = bll.insert(info);
                result = info.ID > 0;
            }
            return RedirectToAction("Info", new { id = info.ID, result = result ? "Y" : "N" });
        }

        [HttpPost]
        public string Delete(string id = "")
        {
            bool result = true;
            foreach (string item in id.Split(',').Where(item => !string.IsNullOrWhiteSpace(item)))
            {
                result = bll.Delete(int.Parse(item));
                if (result == false)
                {
                    break;
                }
            }
            return result.ToString();
        }
    }
}
